package cn.demo.controller;

import cn.demo.Util.Page;
import cn.demo.Util.Result;
import cn.demo.entity.provider;
import cn.demo.service.ProviderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author weizhongli
 * @Date 2020/4/7
 * @Version v1.0
 * @Description 供应商控制管理器
 **/
@Controller
@RequestMapping("/provider")
public class ProviderController {

    //依赖注入service
    @Autowired
    private ProviderService providerService;

    /*
    * 添加供应商
    * */
    @RequestMapping("/addProvider")
    @ResponseBody
    public Result addProvider(provider provider) {
        try {
            int provider1 = providerService.addProvider(provider);
            return new Result(true, "添加成功", provider1);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "添加失败");
        }
    }

    /**
     * 查询所有供应商
     */
    @PreAuthorize("hasAuthority('Bill_select')") //权限控制
    @RequestMapping("/selectProvider")
    @ResponseBody
    public Result providerList(@RequestParam(defaultValue = "1") int PageIndex, @RequestParam(defaultValue = "2") int PageSize, String selectProviderName) {
        try {
            Page page = providerService.selectProviderList(PageIndex, PageSize, selectProviderName);
            return new Result(true, "查询成功", page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }


    /*
    * 根据名字查询供应商
    * */
    @RequestMapping("findByName")
    public Result findByName(String name) {
        try {
            provider provider = providerService.findByName(name);
            return new Result(true, "查询成功", provider);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询失败");
        }
    }

    /*
    * 根据id查询供应商
    * */
    @RequestMapping("/findById")
    public String findById(Integer id, HttpServletRequest request) {
        provider provider = providerService.findById(id);
        request.setAttribute("provider", provider);
        return "providerUpdate";
    }

    /**
     * 修改供应商
     *
     * @param
     * @return
     */
    @RequestMapping("updateProvider")
    @ResponseBody
    public Result updateProvider(provider provider) {
        try {
            int i = providerService.updateProvider(provider);
            return new Result(true, "修改成功", 1);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败");
        }

    }


    /*
     * 根基id删除供应商
     * */
    @RequestMapping("deleteProvider")
    @ResponseBody
    public Result deleteProvider(String str) {
        try {
            String[] ids = str.split(",");
            for (int i = 0; i < ids.length; i++) {
                providerService.deleteProvider(Integer.parseInt(ids[i].trim()));
            }
            return new Result(true, "删除成功", "true");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "删除失败", "false");
        }
    }

    /**
     * 数据回显
     */
    @RequestMapping("/showProviderList")
    public String showBillList() {
        return "providerList";
    }


    /*
     * 导出数据
     * */
    @RequestMapping("/daochu")
    @ResponseBody
    public Result download(HttpServletRequest request) throws Exception {
        List<provider> list = providerService.selectProList();
       /* for (Product item : list) {
            Date birthday = item.getCreationDate();
            SimpleDateFormat myFormat = new SimpleDateFormat("yyyy-MM-dd");
            String format = myFormat.format(birthday);
            item.setCtratedate(format);
        }*/
       request.getSession().setAttribute("list",list);
        return new Result(true,"导出成功","providerEx");
    }

    /*
    * 数据展示
    * */
    @ResponseBody
    @RequestMapping("/providerechart")
    public Result selectProviderEchart() {

        List<Map<String, Object>> maps = providerService.selectCountProList();
        List<String> nameMaps = new ArrayList<String>();
        for (Map<String, Object> map : maps) {
              nameMaps.add((String)map.get("name"));
        }
        HashMap<String, Object> mapCount = new HashMap<String, Object>();
        mapCount.put("proAddress", nameMaps);
        mapCount.put("count", maps);
        return new Result(true, "展示成功", mapCount);

    }
}
